import 'dart:math';

import 'package:flutter/material.dart';
import 'package:jicaimao_flutter/entity/account_bean.dart';
import 'package:jicaimao_flutter/utils/event_bus.dart';
import 'package:jicaimao_flutter/utils/toast.dart';
import 'package:jicaimao_flutter/views/mine/account_details.dart';
import 'package:jicaimao_flutter/views/mine/buy_fail_details.dart';
import 'package:jicaimao_flutter/views/mine/buy_wating_details.dart';
import 'package:jicaimao_flutter/views/mine/expire_datils.dart';
import 'package:jicaimao_flutter/views/mine/hold_datils.dart';
import 'package:jicaimao_flutter/views/mine/redemption_datils.dart';
import 'package:jicaimao_flutter/widget/gradient_circular_progress.dart';
import 'package:jicaimao_flutter/widget/turn_box.dart';

class Transfer_Details extends StatefulWidget {
  final String label;

  @override
  _Transfer_DetailsState createState() => _Transfer_DetailsState();

  Transfer_Details({Key key, this.label}) : super(key: key);
}

class _Transfer_DetailsState extends State<Transfer_Details>
    with AutomaticKeepAliveClientMixin, TickerProviderStateMixin {
  List<AccountBeanResult> mData = List();
  int page = 1; //页码
  int typeFlag = -1; //   -1 全部 0 未到期 1 已完结 2违约
  int textState = -1; //
  int purchaseStatus = -1; //
  String mScreen;

  AnimationController _animationController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _animationController =
        new AnimationController(vsync: this, duration: Duration(seconds: 3));
    bool isForward = true;
    _animationController.addStatusListener((status) {
      if (status == AnimationStatus.forward) {
        isForward = true;
      } else if (status == AnimationStatus.completed ||
          status == AnimationStatus.dismissed) {
        if (isForward) {
          _animationController.reverse();
        } else {
          _animationController.forward();
        }
      } else if (status == AnimationStatus.reverse) {
        isForward = false;
      }
    });
    _animationController.forward();

    mScreen = widget.label;
  }

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context); // See AutomaticKeepAliveClientMixin.
    return _Purchase_Record(context);
  }

  ///申购记录
  Widget _Purchase_Record(BuildContext context) {
    return Container(
      color: Color(0xFFF6F5F5),
      child: Column(
        children: <Widget>[
          GestureDetector(
            child: Container(
              height: 45.0,
              padding: EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 0.0),
              margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 5.0),
              color: Colors.white,
              child: Row(
                children: <Widget>[
                  new Image.asset(
                    'images/img_screen.png',
                    width: 30.0,
                    height: 30.0,
                  ),
                  Expanded(
                    child: new Text(mScreen == null ? "筛选" : mScreen.toString(),
                        style: new TextStyle(
                            fontWeight: FontWeight.normal,
                            fontSize: 15.0,
                            color: Color(0xFF333333))),
                    flex: 1,
                  ),
                  new Image.asset(
                    "images/img_upper.png",
                    width: 12.0,
                    height: 12.0,
                  ),
                ],
              ),
            ),
            onTap: () {
              showSelecDialog(
                context,
              );
            },
          ),
          _ItemData(context, mData),
        ],
      ),
    );
  }

  ///
  void showSelecDialog(
    BuildContext context,
  ) {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return new Scaffold(
            backgroundColor: Color(0xFFF6F5F5),
            appBar: AppBar(
              centerTitle: true,
              backgroundColor: Colors.white,
              leading: IconButton(
                icon: Icon(
                  Icons.arrow_back_ios,
                  color: Colors.grey,
                ),
                onPressed: () => Navigator.pop(context),
              ),
              title: Text(
                '账目',
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.black,
                ),
              ),
            ),
//            body: Container(
            body: Column(
              children: <Widget>[
                Container(
                    height: 50,
                    color: Colors.white,
                    alignment: Alignment.center,
                    child: Column(
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Expanded(
                              child: Container(
                                margin:
                                    EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
                                alignment: Alignment.center,
                                child: Text(
                                  '申购记录',
                                  style: TextStyle(
                                      fontSize: 14, color: Colors.purpleAccent),
                                ),
                              ),
                              flex: 1,
                            ),
                            Expanded(
                              child: Container(
                                margin:
                                    EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
                                alignment: Alignment.center,
                                child: Text(
                                  '转取明细',
                                  style: TextStyle(
                                    fontSize: 14,
                                    color: Colors.black87,
                                  ),
                                ),
                              ),
                              flex: 1,
                            ),
                          ],
                        ),
                        Row(
                          children: <Widget>[
                            Expanded(
                              child: Container(
                                margin:
                                    EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
                                alignment: Alignment.center,
                                child: new Divider(
                                  height: 2,
                                  color: Colors.purpleAccent,
                                ),
                              ),
                              flex: 1,
                            ),
                            Expanded(
                              child: Container(
                                margin:
                                    EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
                                alignment: Alignment.center,
                                child: new Divider(
                                  height: 2,
                                  color: Colors.white,
                                ),
                              ),
                              flex: 1,
                            ),
                          ],
                        ),
                      ],
                    )),
                Container(
                  height: 45.0,
                  padding: EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 0.0),
                  margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 0.0),
                  color: Colors.white,
                  child: Row(
                    children: <Widget>[
                      new Image.asset(
                        'images/img_screen.png',
                        width: 30.0,
                        height: 30.0,
                      ),
                      Expanded(
                        child: new Text(
                            mScreen == null ? "筛选" : mScreen.toString(),
//                            mScreen.toString(),
                            style: new TextStyle(
                                fontWeight: FontWeight.normal,
                                fontSize: 15.0,
                                color: Color(0xFF333333))),
                        flex: 1,
                      ),
                      new Image.asset(
                        "images/img_upper.png",
                        width: 12.0,
                        height: 12.0,
                      ),
                    ],
                  ),
                ),
                CommonItemSelect(
                  onTabItem: () {
                    Navigator.pop(context);
                    mScreen = "全部明细";
                    setState(() {
                      mScreen = "全部明细";
                    });
                  },
                  title: '全部明细',
                  color: Colors.grey,
                  itemHeight: 40.0,
                ),
                CommonItemSelect(
                  onTabItem: () {
                    Navigator.pop(context);
                    mScreen = "持有明细";
                    setState(() {
                      mScreen = "持有明细";
                    });
                  },
                  color: Colors.grey,
                  title: '持有明细',
                  itemHeight: 40.0,
                ),
                CommonItemSelect(
                  title: '赎回明细',
                  itemHeight: 40.0,
                  color: Colors.grey,
                  onTabItem: () {
                    Navigator.pop(context);
                    mScreen = "赎回明细";
                    setState(() {
                      mScreen = "赎回明细";
                    });
                  },
                ),
                CommonItemSelect(
                  title: '到期明细',
                  itemHeight: 40.0,
                  color: Colors.grey,
                  onTabItem: () {
                    Navigator.pop(context);
                    mScreen = "到期明细";
                    setState(() {
                      mScreen = "到期明细";
                    });
                  },
                ),
                CommonItemSelect(
                  title: '受理明细',
                  itemHeight: 40.0,
                  color: Colors.grey,
                  onTabItem: () {
                    Navigator.pop(context);
                    mScreen = "受理明细";
                    setState(() {
                      mScreen = "受理明细";
                    });
                  },
                )
              ],
            ),
          );
        });
  }

  ///申购记录
  Widget _ItemData(BuildContext context, List<AccountBeanResult> list) {
//    if (mData == null || mData.isEmpty) {
//      return Container(
//          margin: EdgeInsets.fromLTRB(150.0, 180.0, 150.0, 0.0),
//          child: new Column(
//            children: <Widget>[
//              Image.asset(
//                'images/no_data.png',
//                width: 80.0,
//                height: 80.0,
//              ),
//              Text('暂无数据')
//            ],
//          ));
//    } else {
//      return listViewListTile(list);
//    }

    return listViewListTile(list);
  }

  ///申购记录
  Widget listViewListTile(List<AccountBeanResult> list) {
    return ListView.builder(
      //设置滑动方向 Axis.horizontal 水平  默认 Axis.vertical 垂直
      scrollDirection: Axis.vertical,

      ///内间距
//      padding: EdgeInsets.all(10.0),
      //是否倒序显示 默认正序 false  倒序true
      reverse: false,
      primary: true,
      //确定每一个item的高度 会让item加载更加高效
//        itemExtent: 50.0,
      shrinkWrap: true,
      //item 数量
//      itemCount: list.length,
      itemCount: 3,
      //滑动类型设置

      physics: new ClampingScrollPhysics(),
      //滑动监听
//        controller ,
      itemBuilder: (context, i) => new InkWell(
            child: new Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Expanded(
                      child: Column(
                        children: <Widget>[
                          Container(
                            alignment: Alignment.centerLeft,
                            color: Colors.white,
                            padding: EdgeInsets.fromLTRB(10.0, 18.0, 0.0, 5.0),
                            child: Row(
                              children: <Widget>[
                                _PurchaseStatus(context),
                                _TextState(context),
                              ],
                            ),
                          ),
                          Container(
                            color: Colors.white,
                            alignment: Alignment.centerLeft,
                            padding: EdgeInsets.fromLTRB(10.0, 5.0, 0.0, 8.0),
                            child: new Text(
//                "${list[i].title}",
                              '数量：200000.0000 BSC',
                              style: new TextStyle(
                                  fontSize: 16.0, color: Color(0xFF333333)),
                            ),
                          ),
                          Container(
                            alignment: Alignment.centerLeft,
                            color: Colors.white,
                            padding: EdgeInsets.fromLTRB(10.0, 5.0, 0.0, 18.0),
                            child: new Text(
//                "${list[i].content}",
                              '2018-06-02 15:23:00',
                              style: new TextStyle(
                                  fontSize: 16.0, color: Color(0xFF999999)),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                        ],
                      ),
                      flex: 3,
                    ),
                    Expanded(
                      child: Container(
                        height: 120,
                        color: Colors.white,
                        child: Column(
                          children: <Widget>[
                            new Stack(
                              children: <Widget>[
                                ///进度条
                                _ProgressBar(context),
                                _Days(context),
                                new Positioned(
                                  left: 77.2,
                                  top: 69.0,
                                  child: Text(
                                    '定期',
                                    style: TextStyle(color: Color(0xFF333333)),
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                      flex: 2,
                    ),
                  ],
                ),
                new Divider(
                  height: 2,
                ),
              ],
            ),
            onTap: () {
//              int tempType = "${list[i].list.getMoneyState}";
//              int tmType = "${list[i].list.getPrincipalType}";
              int tmType = 1;
              int tempType = 0;
              if (tmType == 1) {
                if (tempType == 5) {
                  return;
                }
              ///  投标  到期未提取
                Navigator.push(
                  context,
//                  MaterialPageRoute(builder: (context) => AccountDetails(mData)),
                  MaterialPageRoute(
//                      builder: (context) => BidWattingDetails(list[i].list)),
                      builder: (context) => BidWattingDetails()),
                );
              } else {
                if (tempType == 0) {
                  ///持有
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => HoldDatilsDetails(list[i].list)),
                  );
                } else if (tempType == 1) {
                  ///到期
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => ExpireDatilsDetails(list[i].list)),
                  );
                } else if (tempType == 2 || tempType == 3) {
                  ///赎回
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) =>
                            RedemptionDatilsDetails(list[i].list)),
                  );
                } else if (tempType == 4) {

                  ///审核中
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => BuyWatingDetails(list[i].list)),
                  );
                } else if (tempType == 5) {

                  ///申购失败
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => BuyFailDetails(list[i].list)),
                  );
                }
              }
            },
          ),
    );
  }

  ///进度条
  Widget _ProgressBar(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(0.0, 6.0, 0.0, 0.0),
      child: Wrap(spacing: 10.0, runSpacing: 16.0, children: <Widget>[
        ClipRect(
          child: Align(
            alignment: Alignment.topCenter,
            heightFactor: .8,
            child: Padding(
//          padding: const EdgeInsets.only(bottom: 8.0),
              padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 6.0),
              child: SizedBox(
                width: 120.0,
                height: 103.0,
                child: TurnBox(
                  turns: .60,
                  child: GradientCircularProgressIndicator(
                    colors: [Color(0xFFAD5AFE), Color(0xFFE165C7)],
                    radius: 40.0,
                    stokeWidth: 4.0,
                    value: _animationController.value,
                    totalAngle: 1.6 * pi,
                    strokeCapRound: true,
                  ),
                ),
              ),
            ),
          ),
        ),
      ]),
    );
  }

  Widget _Days(BuildContext context) {
    return Positioned(
      left: 75.2,
      top: 38.0,
      child: Text(
        '180天',
        style: TextStyle(color: Color(0xFFB75CF4)),
      ),
    );
  }

  Widget _PurchaseStatus(BuildContext context) {
    if (purchaseStatus == -1) {
      return Container(
        child: Text(
          '持有',
          style: new TextStyle(
            fontWeight: FontWeight.w600,
            fontSize: 16.0,
            color: Color(0xFF333333),
          ),
        ),
      );
    } else if (purchaseStatus == 0) {
      return Container(
        child: Text(
          '赎回',
          style: new TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 16.0,
              color: Color(0xFF333333)),
        ),
      );
    } else if (purchaseStatus == 1) {
      return Container(
        child: Text(
          '到期',
          style: new TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 16.0,
              color: Color(0xFF333333)),
        ),
      );
    } else if (purchaseStatus == 2) {
      return Container(
        child: Text(
          '受理中',
          style: new TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 16.0,
              color: Color(0xFF333333)),
        ),
      );
    } else {
      return Container(
        child: Text(
          '申购失败',
          style: new TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 16.0,
              color: Color(0xFF333333)),
        ),
      );
    }
  }

  Widget _TextState(BuildContext context) {
    if (textState == -1) {
      return Container(
        margin: EdgeInsets.fromLTRB(10.0, 0.0, 0.0, 0.0),
        child: Text(
//                "${list[i].creatTime}",
          '已审核',
          style: new TextStyle(fontSize: 14.0, color: Color(0xFF63C940)),
        ),
      );
    } else if (textState == 0) {
      return Container(
        margin: EdgeInsets.fromLTRB(10.0, 0.0, 0.0, 0.0),
        child: Text(
//                "${list[i].creatTime}",
          '审核中',
          style: new TextStyle(fontSize: 14.0, color: Color(0xFFF38E3C)),
        ),
      );
    } else if (textState == 1) {
      return Container(
        margin: EdgeInsets.fromLTRB(10.0, 0.0, 0.0, 0.0),
        child: Text(
//                "${list[i].creatTime}",
          '待提取',
          style: new TextStyle(fontSize: 14.0, color: Color(0xFFF38E3C)),
        ),
      );
    } else if (textState == 2) {
      return Container(
        margin: EdgeInsets.fromLTRB(10.0, 0.0, 0.0, 0.0),
        child: Text(
//                "${list[i].creatTime}",
          '已提取',
          style: new TextStyle(fontSize: 14.0, color: Color(0xFF63C940)),
        ),
      );
    } else {
      return Container();
    }
  }
}

class CommonItemSelect extends StatelessWidget {
  CommonItemSelect({this.onTabItem, this.title, this.color, this.itemHeight});

  final String title;
  final Color color;
  final double itemHeight;
  final VoidCallback onTabItem;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTabItem,
      child: Container(
        padding: EdgeInsets.only(left: 20.0, top: 1.0),
        color: Colors.white,
        alignment: Alignment.centerLeft,
        height: itemHeight,
        child: Text(
          title,
          style: TextStyle(
              color: color == null ? Colors.blue : color, fontSize: 16.0),
        ),
      ),
    );
  }
}
